package com.liang.kit;

/**
 * @description 地图工具类
 * @author 龚亮
 * @date 2015-05-25 14:49:35
 */
public class MapDistance {  
	  
    private static double EARTH_RADIUS = 6378137;  
    private static double rad(double d) {  
        return d * Math.PI / 180.0;  
    }
    
	/**
	 * 根据两点间经纬度坐标（double值），计算两点间距离，单位为km
	 * 
	 * @param lng1 经度
	 * @param lat1 纬度
	 * @param lng2 经度
	 * @param lat2 纬度
	 * @return 
	 */
	public static double getDistance(double lng1, double lat1, double lng2,double lat2) {
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000.0;
		String str = s/1000.0+"";
		
		return Double.parseDouble(str);
	}
	
    public static void main(String[] args) {
    	System.out.println(getDistance(115.300699,30.516769,114.400698,30.416767)+"km");
	}
}

